package com.example.controller;


import com.alibaba.dubbo.config.annotation.Reference;

import com.example.domain.message.MessageConstant;
import com.example.domain.pojo.CheckItem;
import com.example.domain.query.QueryPageBean;
import com.example.domain.result.PageResult;
import com.example.domain.result.Result;
import com.example.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController  //controller + responseBody 返回Java对象数据转换json数据返回
@RequestMapping("/checkItem")
public class CheckItemController {
    //注入服务
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 除了分页查询,其他全部返回result实体类
     * @return
     */


    //添加检查项
    @RequestMapping("/add") //@RequestBodyvue提交的数据是json数据,解析提交的json数据,将数据封装为具体的对象
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        //spring异常处理
        try {
            //添加成功,提示成功信息
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }


    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //传递参数
        PageResult page = checkItemService.findPage(queryPageBean.getQueryString(), queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return page;
    }



    @RequestMapping("/deleteCheckItem")
    public Result deleteCheckItem(Integer id){
        //调用方法,删除
        try {
            checkItemService.deleteCheckItem(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }  catch (RuntimeException e) {
            //运行时异常,因为外键关联,无法删除,将报错信息传回
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }
    //根据id查询编辑的体检项,
    @RequestMapping("/findById")
    public Result findCheckItemById(int id){
        try {
            CheckItem checkItemById = checkItemService.findCheckItemById(id);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItemById);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    //根据编辑检查项获取数据,提交到数据库,修改数据库中信息
    @RequestMapping("/edit")
    public Result editCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.editCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return  new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }


}
